package com.yh.workbill.login.mvp;

import android.util.Log;

import com.yh.workbill.db.UserInfoDao;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class LoginPresenter implements LifecycleObserver {
    private LoginView view;
    private LoginModel model;

    public LoginPresenter(LoginView view) {
        this.view = view;
        this.model = new LoginModel();
    }

    public void loginPresenter(UserInfoDao dao, String phone, String pwd) {
        view.showDialog("正在登录...", true);
        model.login(dao, phone, pwd, new LoginModel.OnLoginCallback() {
            @Override
            public void successLogin() {
                view.gonext();
                view.hideDialog();
            }

            @Override
            public void successCheckToken() {

            }

            @Override
            public void fail(String message) {
                view.showTasot(message);
                view.hideDialog();
            }
        });
    }

    public void checkTokenPresenter(String token) {
        view.showDialog("正在检测登录...", true);
        model.chechToken(token, new LoginModel.OnLoginCallback() {
            @Override
            public void successLogin() {

            }

            @Override
            public void successCheckToken() {
                view.gonext();
                view.hideDialog();
            }

            @Override
            public void fail(String message) {

                view.showTasot(message);
                view.hideDialog();
            }
        });
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void recyclear() {
        if (this.view != null) {
            this.view = null;
        }
    }

}
